package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Materiel;
import com.zyd.shiro.business.service.PakMaterielService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakMateriel;
import com.zyd.shiro.persistence.mapper.PakMaterielMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakMaterielServiceImpl implements PakMaterielService {

    @Autowired
    private PakMaterielMapper pakMaterielMapper;

    @Override
    public Materiel insert(Materiel materiel) {
        Assert.notNull(materiel, "对象不可为空！");
        materiel.getPakMateriel().setUpdateTime(new Date());
        materiel.getPakMateriel().setCreateTime(new Date());
        pakMaterielMapper.insertSelective(materiel.getPakMateriel());
        return materiel;
    }

    @Override
    public void insertList(List<Materiel> materiels) {

    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakMaterielMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Materiel materiel) {
        Assert.notNull(materiel, "对象不可为空！");
        materiel.setUpdateTime(new Date());
        return pakMaterielMapper.updateByPrimaryKey(materiel.getPakMateriel()) > 0;
    }

    @Override
    public boolean updateSelective(Materiel materiel) {
        Assert.notNull(materiel, "对象不可为空！");
        materiel.setUpdateTime(new Date());
        return pakMaterielMapper.updateByPrimaryKeySelective(materiel.getPakMateriel()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Materiel getByPrimaryKey(Long primaryKey) {
        PakMateriel pakMateriel = pakMaterielMapper.selectByPrimaryKey(primaryKey);
        return null == pakMateriel ? null : new Materiel(pakMateriel);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Materiel getOneByEntity(Materiel test) {
        Assert.notNull(test, "对象不可为空！");
        PakMateriel pakMateriel = pakMaterielMapper.selectOne(test.getPakMateriel());
        return null == pakMateriel ? null : new Materiel(pakMateriel);
    }

    @Override
    public List<Materiel> listAll() {
        List<PakMateriel> pakMateriels = pakMaterielMapper.selectAll();
        if (CollectionUtils.isEmpty(pakMateriels)) {
            return null;
        }
        List<Materiel> materiels = new ArrayList<>();
        for (PakMateriel pakMateriel : pakMateriels) {
            materiels.add(new Materiel(pakMateriel));
        }
        return materiels;
    }

    @Override
    public List<Materiel> listByEntity(Materiel entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Materiel> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakMateriel> pakMateriels = pakMaterielMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakMateriels)) {
            return null;
        }
        List<Materiel> materiels = new ArrayList<>();
        for (PakMateriel pakMateriel : pakMateriels) {
            materiels.add(new Materiel(pakMateriel));
        }
        PageInfo bean = new PageInfo<PakMateriel>(pakMateriels);
        bean.setList(materiels);
        return bean;
    }
@Override
    public void increase(PakMateriel pakMateriel) {
        pakMaterielMapper.insert(pakMateriel);
    }




}
